package com.jgp.glmeeting.ema.util;


import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SmsStringUtils {

    /**
     * 用于短信模板的替换
     * @param msg
     * @param map
     * @return
     */
    public static String replace(String msg, Map<String, String> map) {
        Pattern pattern = Pattern.compile("\\{([a-zA-z]+)\\}");
        Matcher matcher = pattern.matcher(msg);
        String result = msg;
        while(matcher.find()){
            String key = matcher.group().substring(1,matcher.group().length()-1);
            result = result.replace(matcher.group(), org.apache.commons.lang3.StringUtils.isBlank(map.get(key)) ? "" : map.get(key));
        }
        return result;
    }

    public static void main(String[] args) {
        String msg = "身份证号： {cardNo}, 姓名： {UserName}";
        Map<String, String> map = new HashMap<>();
        map.put("cardNo", "23423423423423");
        map.put("UserName", "初小巍");
        System.out.println(replace(msg, map));
    }
}
